package com.toddsoft.toddtris.control;

import com.toddsoft.toddtris.model.Well;
import com.toddsoft.toddtris.model.WellListener;
import com.toddsoft.util.Repeater;

public class RepeatController extends ToddtrisController 
                              implements WellListener {

    private static final int DEFAULT_SENSITIVITY = 165;
    private static final int DOWN_REPEAT_TIME = 40;
    private static final int LEFT_RIGHT_REPEAT_FACTOR = 3;

    private ToddtrisController wrappedController;
    private Repeater leftRepeater;
    private Repeater rightRepeater;
    private Repeater downRepeater;

    private long leftRightSleepFirstPauseMs;
    private long leftRightSleepRepeatMs;

    
    /**
     * @param delegateTo the controller to delegate to
     */
    public RepeatController(ToddtrisController delegateTo) {
        this(delegateTo, DEFAULT_SENSITIVITY);
    }

    /**
     * @param delegateTo the controller to delegate to
     * @param sensitivity ms pause before first left/right repeat
     */
    public RepeatController(ToddtrisController delegateTo,
                            int sensitivity) {

        super(delegateTo.well);
        this.setSensitivity(sensitivity);
        this.wrappedController = delegateTo;
        this.well.addListener(this);
        leftRepeater = new Repeater() {

                    public void repeat() {
                        RepeatController.this.left();
                    }

                    public long getFirstPauseMs() {
                        return RepeatController.this.leftRightSleepFirstPauseMs;
                    }

                    public long getRepeatMs() {
                        return RepeatController.this.leftRightSleepRepeatMs;
                    }
                };

        rightRepeater = new Repeater() {
                    public void repeat() {
                        RepeatController.this.right();
                    }

                    public long getFirstPauseMs() {
                        return RepeatController.this.leftRightSleepFirstPauseMs;
                    }

                    public long getRepeatMs() {
                        return RepeatController.this.leftRightSleepRepeatMs;
                    }
                };

        downRepeater = new Repeater() {
                    public void repeat() {
                        RepeatController.this.down();
                    }

                    public long getRepeatMs() {
                        return DOWN_REPEAT_TIME;
                    }

                };     
    }

    public void setSensitivity(int pauseMs) {
        this.leftRightSleepFirstPauseMs = pauseMs;
        this.leftRightSleepRepeatMs 
                = (long)(pauseMs / LEFT_RIGHT_REPEAT_FACTOR);
    }
    
    public void startLeft() {
    	this.stopRight();
        this.leftRepeater.startRepeater();
    }
    
    public void stopLeft() {
        this.leftRepeater.stopRepeater();
    }
    
    public void startRight() {
    	this.stopLeft();
        this.rightRepeater.startRepeater();
    }
    
    public void stopRight() {
        this.rightRepeater.stopRepeater();
    }
    
    public void startDown() {
        this.downRepeater.startRepeater();
    }
    
    public void stopDown() {
        this.downRepeater.stopRepeater();
    }
    
    public boolean rotate() {
        return this.wrappedController.rotate();     
    }
    
    public boolean antirotate() {
        return this.wrappedController.antirotate();     
    }
    
    public boolean left() {
        return this.wrappedController.left();     
    }
    
    public boolean right() {
        return this.wrappedController.right();     
    }
    
    public boolean down() {
        return this.wrappedController.down();     
    }
    
    public boolean drop() {
        return this.wrappedController.drop();     
    }
    
    public void restart() {
        this.wrappedController.restart();
    }
    
    public void togglePause() {
        this.wrappedController.togglePause();     
    }
    
    /**
     * @return ActionHandler
     */
    protected ActionHandler getHandler() {
        return this.wrappedController.getHandler();
    }


    /***********************
    * WellListener Methods *
    ***********************/

    /**
     * @param well
     */
    public void blockLanded(Well well) {
        downRepeater.stopRepeater();
    }
    
    /**
     * @param well
     */
    public void wellChanged(Well well) {
        //do nothing
    }
    
    /**
     * @param well
     * @param linesMade
     */
    public void madeLines(Well well, int linesMade) {
        //do nothing
    }

    /**
     * @param well
     */
    public void gameOver(Well well) {
        //do nothing
    }
    
    /**
     * @param well
     */
    public void wellReset(Well well) {
        //do nothing
    }
    
    /**
     * @param well
     */
    public void complete(Well well) {
        //do nothing
    }
}
